package com.example.demo.database

import android.content.Context
import android.util.Log
import androidx.room.Room
import kotlinx.coroutines.CoroutineScope

class AppRoomDatabaseFactory {

    companion object {
        //Singleton prevents multiple instances of database opening at the same time.
        @Volatile
        private var INSTANCE : AppRoomDatabase? = null

        fun createDatabase(context: Context, scope: CoroutineScope): AppRoomDatabase {
            return INSTANCE ?: synchronized(this){
//                val instance = Room.databaseBuilder(context.applicationContext, AppRoomDatabase::class.java,"demo_database.db")
//                    .addCallback(DemoDatabaseCallback(scope))
//                    .build()

                Log.e("AppRoomDatabaseFactory","creating database")

                val instance = Room.inMemoryDatabaseBuilder(context.applicationContext, AppRoomDatabase::class.java)
//                    .apply {
//                        DatabaseCallbackManager.instance.callbacks.forEach {
//                            this.addCallback(it)
//                        }
//                    }
                    .build()

                INSTANCE = instance

                instance
            }
        }
    }
}